package com.example.xd.javatest.threadpool_test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @auther XuDan on 2020/6/18.
 */
public class ThreadPoolClient {
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
        test4();
    }

    /**
     * SingleThreadExecutor:只有一个核心线程,确保所有任务都在一个线程中按顺序执行
     * new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())
     */
    private static void test4() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());

    }

    /**
     * ScheduledThreadPool
     * 核心线程数是指定的,非核心线程数不定,非核心线程数一空闲就会被回收
     * public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService
     * new ScheduledThreadPoolExecutor(corePoolSize);
     * super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue())
     */
    private static void test3() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        scheduledExecutorService.schedule(new MyRun(), 1000, TimeUnit.MILLISECONDS);
        scheduledExecutorService.schedule(new MyRun(), 1000, TimeUnit.MILLISECONDS);
        scheduledExecutorService.schedule(new MyRun(), 1000, TimeUnit.MILLISECONDS);
        scheduledExecutorService.schedule(new MyRun(), 1000, TimeUnit.MILLISECONDS);
        scheduledExecutorService.schedule(new MyRun(), 1000, TimeUnit.MILLISECONDS);
    }

    /**
     * CachedThreadPool
     * 只有非核心线程,线程数可以无限大,当线程池中的线程都处于活动状态时,线程池会创建新的线程来处理任务,
     * 否则会利用空闲线程来处理任务,线程都有超时机制,超过60秒的闲置线程会被回收
     * new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
     */
    private static void test2() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());

    }

    /**
     * FixedThreadPool,只有核心线程且核心线程不会被回收
     * 当所有线程处于活动状态时,新任务会等待状态,知道线程池有空闲线程
     * new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
     */
    private static void test1() {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
        executorService.execute(new MyRun());
    }
}

class MyRun implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getId() + "---" + Thread.currentThread().getName());
    }
}